package com.example.bluejumplib;

import android.util.Log;

import com.example.bluejumplib.utils.StringTool;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class BleReceivedData {
    private static final String TAG = "JumpReceviceData";
    private String vendorId;
    private int data;
    private int time;
    private int batteryLevel;
    private int version;
    private int type=0;

    public BleReceivedData(byte[] datas) {
        if (datas.length == 9) {
            // 将hex字符串转换为字节数组
            // 解析字节数组中的各个字段
            vendorId = byteArrayToHexString(Arrays.copyOfRange(datas, 0, 2));
            version = datas[2] & 0xFF;
            batteryLevel = datas[3] & 0xFF;
//        time = bytesToLittleEndianInt(Arrays.copyOfRange(datas, 4, 6));
//        time = Integer.parseInt(StringTool.hexToTen(byteArrayToHexString(Arrays.copyOfRange(datas, 4, 6))));
            data = bytesToBigEndianInt(Arrays.copyOfRange(datas, 4, 6));
            time = bytesToBigEndianInt(Arrays.copyOfRange(datas, 7, 9));
            type=0;
        }
        if (datas.length == 6) {
            //05FF4B3510 00
            type=1;
            vendorId = byteArrayToHexString(Arrays.copyOfRange(datas, 0, 2));
            version = datas[2] & 0xFF;
            batteryLevel = datas[3] & 0xFF;
            data=datas[5] & 0xFF;
        }
//        time = datas[8] & 0xFF; // 保持unsigned
    }

    // 将hex字符串转换为字节数组
    private static byte[] hexStringToByteArray(String hex) {
        int len = hex.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }

    // 将byte数组转换为hex字符串
    private static String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    // 将little-endian字节序的数组转换为int,小端数据
    private static int bytesToLittleEndianInt(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < bytes.length; i++) {
            value |= (bytes[i] & 0xFF) << (i * 8);
        }
        return value;
    }

    //大端数据
    private static int bytesToBigEndianInt(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < bytes.length; i++) {
            value |= (bytes[i] & 0xFF) << (((bytes.length - 1) - i) * 8);
        }
        return value;
    }

    private static int bytesToBigEndianInt2(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.BIG_ENDIAN);
        return buffer.getInt();
    }

    // getters and setters
    public String getVendorId() {
        return vendorId;
    }

    public int getData() {
        return data;
    }

    public int getTime() {
        return time;
    }

    public int getBatteryLevel() {
        return batteryLevel;
    }

    public int getVersion() {
        return version;
    }

    @Override
    public String toString() {
        return "JumpReceviceData{" +
                "vendorId='" + vendorId + '\'' +
                ", data=" + data +
                ", time=" + time +
                ", batteryLevel=" + batteryLevel +
                ", version=" + version +
                '}';
    }
}

